layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

/*    layer.config({
        extend: 'myskin/style.css', //加载您的扩展样式
        skin: 'my_skin'
    });*/
    //学校列表
    var tableIns = table.render({
        elem: '#bjxxList',
        url : '/bjxxList',
        cellMinWidth : 95,
        // width: '1000',
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        id : "bjxxListTable",
        cols : [[
        {type: "checkbox", fixed:"left", width:50},
        {type: "numbers", title: '序号',fixed:"left", width:50},
        {field: 'id', title: 'Id', minWidth:0, width:0,hide:true},
        {field: 'school_name', title: '学校', minWidth:120, align:"center"},
        {field: 'bjbh', title: '班级编号', minWidth:80, align:"center"},
        {field: 'name', title: '名称', minWidth:80, align:"center"},
        {field: 'short_name', title: '简称', minWidth:150, align:'center'},
        {field: 'nj', title: '年级', minWidth:120, align:'center'},
        //{field: 'rs', title: '人数', minWidth:120, align:'center'},
        //{field: 'bzr', title: '班主任', minWidth:120, align:'center'},
        {title: '操作', minWidth:190,fixed:"right",align:"center",templet:"#bjxxListBar"}
        ]],
        done: function () {
            // $("[data-field='id']").css('display','none');
        }
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != '' || $("#xxxxId").val() !=''){
            table.reload("bjxxListTable",{
                url:"/bjxxList",
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    name: $(".searchVal").val(),
                    xxxx_id: $("#xxxxId").val()//搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加班级
    function addBjxx(edit){
        var title = "添加班级";
        var url = "/toBjxxAdd";
        if (edit) {
            title = "编辑班级";
            url="/toBjxxModify?id="+edit.id;
        }
        var index = layui.layer.open({
            title : title,
            type : 2,
            content : url,
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回班级列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    $(".addNews_btn").click(function(){
        addBjxx();
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('bjxxListTable'),
            data = checkStatus.data,
            id = "";
        if(data.length > 0) {
            for (var i in data) {
                if(i==data.length-1){
                    id+=data[i].id;
                }else{
                    id+=data[i].id+",";
                }
            }
            layer.confirm('确定删除选中的班级？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/bjxxDel",{
                    id : id //将需要删除的newsId作为参数传入
                },function(data){
                    if (data =="success") {
                        tableIns.reload();
                        layer.close(index);
                    }else if (data == "error") {
                        layer.msg("班级删除失败！");
                    }
                    else if(data=="isExistStu"){
                        layer.msg("班级中存在学生，删除失败！");
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的班级");
        }
    });

    //列表操作
    table.on('tool(bjxxList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addBjxx(data);
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此班级？',{icon:3, title:'提示信息'},function(index){
                $.get("/bjxxDel",{
                    id : data.id //参数传入
                },function(data){
                    if (data =="success") {
                        tableIns.reload();
                        layer.close(index);
                    }else if (data == "error") {
                        layer.msg("班级删除失败！");
                    }
                    else if(data=="isExistStu"){
                        layer.msg("学校下存在班级，删除失败！");
                    }

                })
            });
        }
    });

})
